<template>
  <el-dialog :visible.sync="visible" custom-class="dialog_default" title="异常核查分配">

    <ex_super_form ref="form" v-bind="form_option">



    </ex_super_form>
    <span slot="footer" class="dialog_footer">
      <el-button @click="visible = false">{{ $tt('label_cancel') }}</el-button>
      <el-button type="primary" @click="submit">
        {{ $tt('label_submit') }}
      </el-button>
    </span>
  </el-dialog>
</template>

<script>

export default {
  components: {},
  route_manual: true,
  data() {
    return {
      visible: false,
      oms_users_list: [],
      disabled_oms: [],
      wms_warehouse_list: [],
      disabled_wms: [],

      disabled_tms: false,

      form_option: {
        default: {
          oms_users: [],
          wms_warehouse: [],
          is_tms: false,
        },
        model: [
          {
            key: "orderList", label: "订单编码", formType: "text", formatter: (r, c, v) => {
              return v ? v.map(it => it.code).join(',') : ''
            }
          },
          {
            label: 'label_check_allocation', render: (h, { data }) => {
              return (<div class="flex_row" >
                <el-checkbox-group value={data.oms_users} class="card mg_h1" style="flex:1;"
                  onInput={(e) => {
                    this.$set(data, 'oms_users', e)
                    console.log(data)
                  }}>
                  <ex_header_spiler title="OMS人员" />
                  <div style="height:160px;overflow-y: auto;">
                    {
                      this.oms_users_list.map(u => {
                        return <el-checkbox class="mg_l2" label={u.userId} disabled={this.disabled_oms.includes(u.userId)}
                          border size="mini">
                          {u.userName}
                        </el-checkbox>
                      })
                    }
                  </div>

                </el-checkbox-group>
                <el-checkbox-group value={data.wms_warehouse} class="card mg_h1" style="flex:1;"
                  onInput={(e) => {
                    this.$set(data, 'wms_warehouse', e)
                    console.log(data)
                  }}>
                  <ex_header_spiler title="WMS仓库" />
                  <div style="height:160px;overflow-y: auto;">
                    {
                      this.wms_warehouse_list.map(u => {
                        return <el-checkbox class="mg_l2" label={u.id} border size="mini"
                          disabled={this.disabled_wms.includes(u.id)}
                        >
                          {u.name}
                        </el-checkbox>
                      })
                    }
                  </div>
                </el-checkbox-group>
                <div class="card mg_h1" style="flex:1;">
                  <ex_header_spiler title="TMS系统" />
                  <el-checkbox class="mg_l2" label="TMS系统" value={data.is_tms} border size="mini"
                    disabled={this.disabled_tms}
                    onInput={(e) => {
                      this.$set(data, 'is_tms', e)
                    }}
                  ></el-checkbox>
                </div>
              </div>)
            }
          },
          // {
          //   key: 'users', label: 'label_check_user', type: "common_user_select", attrs: {
          //     multiple: true,
          //   },
          //   keyExtra: {
          //     changeItem: (e, data) => {
          //       if (!e) e = []
          //       this.$set(data, 'authorityAllotList', e.map(it => {
          //         return {
          //           checkUserId: it.userId,
          //           checkUserCode: it.userCode,
          //           checkUserName: it.userName,
          //         }
          //       }))
          //     }
          //   },
          // },
          // {
          //   key: 'warehouse', label: '核查仓库', type: "base_warehouse_select", attrs: { multiple: true, },
          //   keyExtra: {
          //     change: (e, data) => {
          //       if (!e) e = []
          //       this.$set(data, 'dataAllotList', e.map(it => {
          //         return {
          //           warehouseId: it.id,
          //           warehouseCode: it.code,
          //           warehouseName: it.name,
          //           sourceSystem: "WMS"
          //         }
          //       }))
          //     }
          //   },
          // },
          // {
          //   key: 'tms', label: '核查系统', render: (h, { data }) => {
          //     return (<el-checkbox value={data.tms} label="TMS" border onInput={(e) => this.$set(data, 'tms', e)}></el-checkbox>)
          //   }
          // },
        ],
        rules: {
          'users': { required: true, type: 'array', message: this.$tt('tip_can_no_empty'), trigger: 'blur' },
        },
      },

    }
  },
  props: {

  },
  methods: {

    show(rows) {
      this.visible = true
      const initValue = {
        orderList: rows,
        oms_users: [],
        wms_warehouse: [],
        is_tms: rows.some(it => it.anomalySourceSystem == 'TMS'),
      }
      this.$nextTick(() => {
        this.disabled_tms = initValue.is_tms
        this.$refs.form.setData(initValue)
      })
      this.$storeUrlCache({
        url: "/ums-api/user/UserInfoRpc/getUserDict.json",
        params: {},
        success: (list) => {
          this.oms_users_list = list
          const select_o = rows.filter(it => list.some(u => u.userId == it.reportUserId)).map(it => it.reportUserId)
          this.$nextTick(() => {
            this.disabled_oms = [...select_o]
            this.$refs.form.set("oms_users", [...select_o])
          })
        }
      })
      this.$storeUrlCache({
        url: "/oms-api/base/OmsBaseWarehouseRpc/selectAllLabel.json",
        params: {},
        success: (list) => {
          this.wms_warehouse_list = list
          const select_w = rows.filter(it => {
            return it.anomalySourceSystem == 'WMS' && list.some(w => w.id == it.warehouseId)
          }).map(it => it.warehouseId)
          this.$nextTick(() => {
            this.disabled_wms = [...select_w]
            this.$refs.form.set("wms_warehouse", [...select_w])
          })
        }
      })

    },
    submit() {
      let { is_tms, oms_users, wms_warehouse, orderList } = this.$refs.form.getData()
      if (!oms_users) oms_users = []
      if (!wms_warehouse) wms_warehouse = []
      if (!orderList) orderList = []
      let authorityAllotList = oms_users.map(it => {
        const o = _.find(this.oms_users_list, user => user.userId == it)
        if (!o) return {}
        return {
          checkUserId: o.userId,
          checkUserCode: o.userCode,
          checkUserName: o.userName,
        }
      }).filter(it => it.checkUserId)
      let dataAllotList = wms_warehouse.map(it => {
        const w = _.find(this.wms_warehouse_list, wh => wh.id == it)
        if (!w) return {}
        return {
          warehouseId: w.id,
          warehouseCode: w.code,
          warehouseName: w.name,
          sourceSystem: 'WMS',
        }
      }).filter(it => it.warehouseId)
      if (is_tms) {
        dataAllotList = [...dataAllotList, { sourceSystem: 'TMS' }]
      }
      const data = {
        anomalyOrderList: orderList.map(it => {
          return {
            anomalyId: it.id,
            anomalyCode: it.code
          }
        }),
        authorityAllotList,
        dataAllotList,
      }
      this.$ajax({
        url: "/oms-api/anomaly/OmsBaseAnomalyRpc/allot.json",
        method: 'post', type: "json",
        loading: true,
        data,
        success: (res) => {
          this.$message({
            showClose: true,
            message: this.$tt('tip_submit_success'),
            type: 'success'
          })
          this.visible = false
          this.$emit('success', true)
        }
      })
    },
  }
}
</script>
